package com.printeverywhere.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.HibernateException;
import org.hibernate.exception.ConstraintViolationException;

import com.printeverywhere.delegator.CrossPromotionServiceDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.dto.CrossPromotion;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

public class CrossPromotionAction extends FrontBaseSupport implements ServletRequestAware{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private CrossPromotion crossPromotion;
	private List<CrossPromotion> crossPromotionList;
	private List<Items> itemsList;
	private User user;
	private int itemId;
	private int id;
	private HttpServletRequest request;
	private String type;
	public CrossPromotionAction(){
		super(true, false);
		setMyAccount(true);
	}

	public String execute() throws Exception{
		try{
			user = SessionUtil.getUser(request);
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute(AdminConstants.USERID, user);
			itemsList = ItemsServiceDelegator.getList(searchCriteria);
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AdminConstants.USER, user);
			crossPromotionList = CrossPromotionServiceDelegator.getList(searchCriteria);
		}catch (Exception e) {
			addActionError("Problem in cross promotion.");
		}
		return SUCCESS;
	}

	public String addCrossPromotion(){
		if (itemId > 0) {
			try {
				user = SessionUtil.getUser(request);
				Items items = ItemsServiceDelegator.getItemsById(itemId);
				crossPromotion = new CrossPromotion();
				crossPromotion.setItems(items);
				crossPromotion.setUser(user);
				CrossPromotionServiceDelegator.add(crossPromotion);
			} catch (ConstraintViolationException e) {
				addActionError("Selected item not added to cross promotion.");
				return SUCCESS;
			}
		}
		return SUCCESS;
	}


	public String addItemsToCrossPromotion(){
		try {
			if(request.getParameter(AdminConstants.TYPE)!= null){
				type = request.getParameter(AdminConstants.TYPE);
			}
			if (request.getParameter("check_list") != null) {
				String[] checkedId = request.getParameterValues("check_list");
				for (String itemId : checkedId) {
					this.itemId = Integer.valueOf(itemId);
					if (this.itemId > 0) {
						try{
							user = SessionUtil.getUser(request);
							Items items = ItemsServiceDelegator
									.getItemsById(this.itemId);
							crossPromotion = new CrossPromotion();
							crossPromotion.setItems(items);
							crossPromotion.setUser(user);
							CrossPromotionServiceDelegator.add(crossPromotion);
						}catch (HibernateException e) {
							continue;
						}
					}
				}
			}
			if(type.equalsIgnoreCase(AttributeConstants.ITEMTYPE_STRAIGHT_AUCTION)){
				return SUCCESS;
			}else if(type.equalsIgnoreCase(AttributeConstants.ITEMTYPE_FIXED_PRICE)){
				return AttributeConstants.ITEMTYPE_FIXED_PRICE;
			}else if(type.equalsIgnoreCase(AttributeConstants.ITEMTYPE_SHOP)){
				return AttributeConstants.ITEMTYPE_SHOP;
			}
		}catch (Exception e) {
			return SUCCESS;
		}
		return SUCCESS;
	}
	public String deleteCrossPromotion(){
		try{
			crossPromotion = CrossPromotionServiceDelegator.getCrossPromotionById(id);
			CrossPromotionServiceDelegator.delete(crossPromotion);
		}catch (Exception e) {
			return SUCCESS;
		}
		return SUCCESS;
	}


	public CrossPromotion getCrossPromotion() {
		return crossPromotion;
	}

	public void setCrossPromotion(CrossPromotion crossPromotion) {
		this.crossPromotion = crossPromotion;
	}

	public List<CrossPromotion> getCrossPromotionList() {
		return crossPromotionList;
	}

	public void setCrossPromotionList(List<CrossPromotion> crossPromotionList) {
		this.crossPromotionList = crossPromotionList;
	}

	public List<Items> getItemsList() {
		return itemsList;
	}

	public void setItemsList(List<Items> itemsList) {
		this.itemsList = itemsList;

	}
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
